package com.seventh.parkingrent.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seventh.parkingrent.commens.Result;
import com.seventh.parkingrent.controller.form.ParkingSpaceFormForFront;
import com.seventh.parkingrent.dao.UserMapper;
import com.seventh.parkingrent.domain.ParkingSpace;
import com.seventh.parkingrent.domain.PropertyManager;
import com.seventh.parkingrent.domain.User;
import com.seventh.parkingrent.service.IParkingSpaceAuditService;
import com.seventh.parkingrent.service.IParkingSpaceService;
import com.seventh.parkingrent.service.IPropertyManagerService;
import com.seventh.parkingrent.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 车位信息表 前端控制器
 * </p>
 *
 * @author Lucas
 * @since 2021-08-25
 */
@RestController
@Slf4j
@CrossOrigin("*")
public class ParkingSpaceController {
    @Resource
    private IParkingSpaceAuditService parkingSpaceAuditService;

    @Resource
    private IParkingSpaceService parkingSpaceService;

    @Resource
    private IUserService userService;

    @Resource
    private IPropertyManagerService propertyManagerService;

    @GetMapping("/getParkingSpaceByObject")
    public Result getParkingSpaceByObject(String username,String propertyName,String parkingSpaceAddr,
                                            @RequestParam(name = "pageIndex",defaultValue = "1")Integer pageIndex,
                                            @RequestParam(name="pageSize",defaultValue = "3")Integer pageSize){
        log.info("获得前端搜索车位的username={},propertyName={},parkingSpaceAddr={},pageIndex={},pageSize={}",username,propertyName,parkingSpaceAddr,pageIndex,pageSize);
        IPage<ParkingSpaceFormForFront> parkingSpaceIPage = parkingSpaceService.selectParkingSpaceByObjectLike(username,propertyName,parkingSpaceAddr, pageIndex, pageSize);
        return Result.success(parkingSpaceIPage);
    }


    @GetMapping("/getNeedAuditParkingSpace")
    public Result getNeedAuditParkingSpace(@RequestParam(name="pageIndex",defaultValue = "1") Integer pageIndex,
                                           @RequestParam(name="pageSize",defaultValue = "3") Integer pageSize){
        log.info("前端提请需审核的停车场pageIndex={},pageSize={}",pageIndex,pageSize);

        IPage<ParkingSpaceFormForFront> parkingSpaces=parkingSpaceService.getNeedAuditParking(pageIndex,pageSize);

        if (null==parkingSpaces) {
            return Result.success(parkingSpaces);
        }
        for (ParkingSpaceFormForFront parkingSpace : parkingSpaces.getRecords()) {
            Integer userId = parkingSpace.getUserId();
            User user = userService.selectUserById(userId);
            parkingSpace.setUsername(user.getUsername());

            Integer propertyManagerId = parkingSpace.getPropertyManagerId();
            PropertyManager propertyManager = propertyManagerService.selectPropertyManagerById(propertyManagerId);
            parkingSpace.setPropertyName(propertyManager.getPropertyName());
        }

        return Result.success(parkingSpaces);

    }

    //获取需要审核的停车场数量
    @GetMapping("/getNeedAuditParkingNo")
    public Result getNeedAuditParkingNo(){
        log.info("前台提请需审核的停车场数量");
        Integer needAuditParkingNo=parkingSpaceService.getNeedAuditParkingNo();

        return Result.success(needAuditParkingNo);
    }
    //获得停车场地址
    @GetMapping("/getAddr")
    public Result getAddr(){
        log.info("前端提请停车场所在地址");
        List<String> parkingSpaceAddrs = parkingSpaceService.selectParkingSpaceAddr();
        return Result.success(parkingSpaceAddrs);

    }


    public Result getBindingResult(BindingResult bindingResult){
        List<FieldError> fieldErrors = bindingResult.getFieldErrors();
        StringBuilder stringBuilder = new StringBuilder();
        for (FieldError fieldError : fieldErrors) {
            stringBuilder.append(fieldError.getField()+"："+fieldError.getDefaultMessage()+";");
        }
        return Result.fail("10086",stringBuilder.toString());
    }
}

